// pages/user/integral/buy-goods/buy.js
import nationData from '../../../../utils/data.js'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:-1,
    name: '',
    phone: '',
    area: '请选择',
    address: '',
    isShowArea: false,
    householdData: nationData
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    token = getApp().globalData.token;
    this.data.id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  onInputName(e) {
    this.data.name = e.detail.value;
  },
  onInputPhone(e) {
    this.data.phone = e.detail.value;
  },
  onInputAddress(e) {
    this.data.address = e.detail.value;
  },

  onClickAera() {
    this.setData({
      isShowAera: true
    });
  },

  onCloseHousehlod() {
    this.setData({
      isShowAera: false
    });
  },

  onChangeAera(e) {
    this.setData({
      area: e.detail.options.map((n) => n.label).join('-')
    });
  },

  onSubmitBtn() {
    let obj = {};
    obj.receiver = this.data.name;
    obj.mobile = this.data.phone;
    obj.province = this.data.area.split('-')[0];
    obj.city = this.data.area.split('-')[1];
    obj.district = this.data.area.split('-')[2];
    obj.detailAddress = this.data.address;
    let isError = false;
    for (const key in obj) {
      if (!obj[key] || obj[key] == '请选择') {
        isError = true;
      }
    }
    if (isError) {
      T.toast({
        id: '#toast',
        text: '还有选项没有填写或者选择,请检查'
      })
      return
    }
    if (!T.regExp.tel.test(obj.mobile)) {
      T.toast({
        id: '#toast',
        text: '手机号格式不正确,请检查'
      })
      return;
    }
    obj.goodsId = this.data.id;
    T.req({
      loadTitle:'提交中...',
      url: config.api + '/point/goods/address?token=' + token,
      method:'POST',
      data:obj,
      success: (res) => {
          T.toast({id:'#toast', text:'提交成功', success: () => {
            wx.navigateBack({
              delta: 1
            })
          }})
      }
    })
  }
})